let App = getApp(),
  wxParse = require("../../wxParse/wxParse.js");

const computedBehavior = require('miniprogram-computed')

Page({
  behaviors: [computedBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    status: 1,
    nav_select: false, // 快捷导航
    indicatorDots: true, // 是否显示面板指示点
    indicatorActiveColor: 'rgba(255,255,255, .8)',
    autoplay: true, // 是否自动切换
    interval: 3000, // 自动切换时间间隔
    duration: 800, // 滑动动画时长
    currentIndex: 1, // 轮播图指针
    floorstatus: false,
    showView: false, // 显示日期选择

    // 详情信息
    goods_id: '',
    detail: {
      id: 1,
      name: "地摊",
      startAt: "2020-09-01",
      endAt: "2020-09-07",
      price: 10, // 价格
      content: "此地人流量大，交通便利；干净整洁"
    },
    banners: [],
    dateList: [{
        date: "2020-09-09",
        value: 0
      }, {
        date: "2020-09-09",
        value: 0
      },
      {
        date: "2020-09-09",
        value: 0
      }, {
        date: "2020-09-09",
        value: 0
      },
      {
        date: "2020-09-09",
        value: 0
      }, {
        date: "2020-09-09",
        value: 0
      },
    ],
    selected: [],
  },
  /**
   * 计算属性
   */
  computed: {
    totalPrice: function (data) {
      let days = data.selected.length;
      console.log("days => ", days);
      return days * data.detail.price;
    },
    selectedList: function (data) {
      let list = [];
      data.selected.forEach((v) => {
        list.push(data.dateList[v]);
      })
      console.log("selected: ", data.selected);
      return list;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 获取id
    this.setData({
      goods_id: options.goods_id
    });
    // 获取商品信息
    this.getGoodsDetail();
  },
  onShow() {
    this.setData({
      status: App.getStatus()
    });
  },
  /**
   * 获取详细信息
   */
  getGoodsDetail() {
    let _this = this;
    App._get('goods/detail', {
      id: _this.data.goods_id
    }, function (result) {
      // 初始化商品详情数据
      console.log(result.data);
      let data = _this.initGoodsData(result.data);
      _this.setData(data);
    });
  },
  /**
   * 初始化商品详情数据
   */
  initGoodsData(data) {
    return {
      banners: data.banners,
      detail: data.detail,
      dateList: data.dateList
    };
  },
  /**
   * 设置轮播图当前指针 数字
   */
  setCurrent(e) {
    this.setData({
      currentIndex: e.detail.current + 1
    });
  },
  /**
   * 控制日期选择显示隐藏
   */
  onChangeShowState() {
    // 发送请求获取日期
    // 
    this.setData({
      showView: !this.data.showView
    });
  },
  /**
   * 点击蒙版，关闭日期选择
   */
  onTouchMask(e) {
    console.log("e => ", e);

    this.setData({
      showView: false,
      selected: e.detail.selected
    });
  },
  onTapSubmit(e) {
    this.setData({
      showView: false,
      selected: e.detail.selected
    })
  },
  onTapCancel(e) {
    this.setData({
      showView: false,
      selected: e.detail.selected
    })
  },
  /**
   * 返回顶部
   */
  goTop(t) {
    this.setData({
      scrollTop: 0
    });
  },
  /**
   * 显示/隐藏 返回顶部按钮
   */
  scroll(e) {
    this.setData({
      floorstatus: e.detail.scrollTop > 200
    })
  },
  /**
   * 立即租赁
   */
  submit(e) {
    const _this = this;
    // 校验
    if (_this.data.status == 1) {
      App.showError("请先完成实名认证", function () {
        wx.navigateTo({
          url: '/pages/user/auth',
        });
      })
    } else if (_this.data.status == 2) { 
      App.showError("请等待实名认证审核通过")
    } else if (this.data.selected.length == 0) {
      App.showError("请先选择租赁时间！");
    } else {
      // 创建订单，并支付
      wx.showLoading({
        title: '正在生成订单...',
      });
      // 订单创建成功后回调--微信支付
      const callback = function () {
        wx.requestPayment({
          nonceStr: 'nonceStr',
          package: 'package',
          paySign: 'MD5',
          timeStamp: 'timeStamp',
          success: (res) => {
            // 跳转订单页
            wx.navigateTo({
              url: '/pages/order/index?type=success',
            })
          },
          fail: (err) => {
            App.showError("订单未支付，微信支付调用失败", () => {
              wx.navigateTo({
                url: '/pages/order/index?type=failure',
              })
            })
          }
        })
      }
      // 发送请求生成订单
      let dateList = [];
      _this.data.selected.forEach(d => {
        dateList.push(_this.data.dateList[d]);
      });
      App._post_form("order/add", {
        goodsId: _this.data.goods_id,
        dateList: dateList
      }, function (res) {
        callback(res);
      }, function (err) {

      });

    }

  },
})